﻿using System;
using System.Collections.Generic;
using UnityEngine;

// Token: 0x0200041F RID: 1055
public class CheckDynamicCollision : MonoBehaviour
{
	// Token: 0x06001DD4 RID: 7636 RVA: 0x000B736C File Offset: 0x000B556C
	private void Start()
	{
		FracturedObject component = base.GetComponent<FracturedObject>();
		if (component != null)
		{
			if (component.GetComponent<Collider>() != null)
			{
				component.GetComponent<Collider>().enabled = true;
			}
			else
			{
				Debug.LogWarning("Fracturable Object " + base.gameObject.name + " has a dynamic rigidbody but no collider. Object will not be able to collide.");
			}
			for (int i = 0; i < component.ListFracturedChunks.Count; i++)
			{
				this.EnableObjectColliders(component.ListFracturedChunks[i].gameObject, false);
			}
		}
	}

	// Token: 0x06001DD5 RID: 7637 RVA: 0x000B7404 File Offset: 0x000B5604
	private void OnCollisionEnter(Collision collision)
	{
		if (collision.contacts == null)
		{
			return;
		}
		if (collision.contacts.Length == 0)
		{
			return;
		}
		FracturedObject component = base.gameObject.GetComponent<FracturedObject>();
		if (component != null)
		{
			float num = (!(collision.rigidbody != null)) ? float.PositiveInfinity : collision.rigidbody.mass;
			if (collision.relativeVelocity.magnitude > component.EventDetachMinVelocity && num > component.EventDetachMinVelocity)
			{
				component.GetComponent<Collider>().enabled = false;
				Rigidbody component2 = component.GetComponent<Rigidbody>();
				if (component2 != null)
				{
					component2.isKinematic = true;
				}
				for (int i = 0; i < component.ListFracturedChunks.Count; i++)
				{
					this.EnableObjectColliders(component.ListFracturedChunks[i].gameObject, true);
				}
				component.Explode(collision.contacts[0].point, collision.relativeVelocity.magnitude);
			}
		}
	}

	// Token: 0x06001DD6 RID: 7638 RVA: 0x000B7514 File Offset: 0x000B5714
	private void EnableObjectColliders(GameObject chunk, bool bEnable)
	{
		List<Collider> list = new List<Collider>();
		CheckDynamicCollision.SearchForAllComponentsInHierarchy<Collider>(chunk, ref list);
		for (int i = 0; i < list.Count; i++)
		{
			list[i].enabled = bEnable;
			if (bEnable)
			{
				list[i].isTrigger = false;
			}
		}
	}

	// Token: 0x06001DD7 RID: 7639 RVA: 0x000B7568 File Offset: 0x000B5768
	private static void SearchForAllComponentsInHierarchy<T>(GameObject current, ref List<T> listOut) where T : Component
	{
		T component = current.GetComponent<T>();
		if (component != null)
		{
			listOut.Add(component);
		}
		for (int i = 0; i < current.transform.childCount; i++)
		{
			CheckDynamicCollision.SearchForAllComponentsInHierarchy<T>(current.transform.GetChild(i).gameObject, ref listOut);
		}
	}
}
